﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YAMN_Cinemas.Presentation
{
    public partial class SeatBooking : System.Web.UI.Page
    {
        int chargePerSheet = 200;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSeats();
            }     
        }
        void BindSeats()
        {
            txtAmount.Text = chargePerSheet.ToString();
            TheatreEntities te = new TheatreEntities();
            long screenID = Convert.ToInt64(Request.Params["screenID"]);
            long STID = Convert.ToInt64(Request.Params["STID"]);
            long movieID = Convert.ToInt64(Request.Params["movieID"]);
            DateTime movieDate = Convert.ToDateTime(Request.Params["movieDate"]);
            
            
           
            String sel = String.Join(",", (from b in te.Bookings
                                           join s in te.SeatDetails on b.BookingID equals s.BookingID
                                           where s.ScreenID == screenID && s.STID == STID && s.MovieDate == movieDate && s.MovieID == movieID
                                           select s.SeatID).ToArray());
                                           
                                          
            ClientScript.RegisterStartupScript(this.GetType(), "Init", "init([" + sel + "]);", true);

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            long screenID = Convert.ToInt64(Request.Params["screenID"]);
            long movieID = Convert.ToInt64(Request.Params["movieID"]);
            long userID = Convert.ToInt64(Request.Params["userdID"]);
            long STID = Convert.ToInt64(Request.Params["STID"]);
            string username = HttpContext.Current.User.Identity.Name.ToString();
            DateTime movieDate = Convert.ToDateTime(Request.Params["movieDate"]);
            
            TheatreEntities te = new TheatreEntities();
            Booking objBooking = new Booking(); 
           
            objBooking.Name = txtName.Text;
            objBooking.PhoneNumber = Convert.ToInt32(txtPhone.Text);

            Console.WriteLine(txtAmount.Text);
            String s = txtAmount.ClientID;
            Console.WriteLine(float.Parse(Request.Form[txtAmount.ClientID]));
            

            objBooking.Amount = float.Parse(Request.Form[txtAmount.ClientID]); //must check later
            objBooking.UserID = username;
            objBooking.MovieID = movieID;

            objBooking.BookingDate = DateTime.Now;

            
            string[] seats = Request.Form[txtSeatNo.ClientID].Split(new char[] { ',' });
            for (int i = 0; i < seats.Length; i++)
            {
                objBooking.SeatDetails.Add(new SeatDetail() { SeatID = Convert.ToInt32(seats[i]), ScreenID = screenID, STID = STID, MovieDate = movieDate, MovieID = movieID });
   
            }
            te.Bookings.AddObject(objBooking);
            te.SaveChanges();
            BindSeats();
            lbl_Status.Visible = true;
            lbl_Status.Text = "Your Seats Have Been Booked Successfully";
           // btn_Final.Visible = true;
            visible();
            var y = from m in te.Movies
                    where m.MovieID == movieID
                    select m.Movie_Title;
            
            //txt_MovieName.Text = y.ToString();
            txt_BookingDate2.Text = movieDate.ToShortDateString();
            List<Movy> movie = new List<Movy>();
            movie = (from m in te.Movies
                     where m.MovieID == movieID
                     select m).ToList();
            Movy mnew = movie.First();
            txt_ScreenName.Text = mnew.Screen.Screen_Name.ToString();
            txt_MovieName.Text = mnew.Movie_Title.ToString();

            //Booking Table
            List<long> BID = new List<long>();
            BID = (from b in te.Bookings
                   select b.BookingID).ToList();
            long bkid = BID.Last();
            List<Booking> newbk = new List<Booking>();
            newbk = (from b in te.Bookings
                     where b.BookingID == bkid
                     select b).ToList();
            Booking binfo = newbk.First();
            txt_AmountPaid.Text = binfo.Amount.ToString();
            



            
           
           
          
        }
        public void visible()
        {
            lbl_AmountPaid.Visible = true;
            lbl_BookingDate.Visible = true;
            lbl_MovieName.Visible = true;
            lbl_ScreenName.Visible = true;
            lbl_Status.Visible = true;

            txt_AmountPaid.Visible = true;
            txt_BookingDate2.Visible = true;
            txt_MovieName.Visible = true;
            txt_ScreenName.Visible = true;

        }

        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            string username = Request.Params["username"].ToString();
            string url = "User_SelectMovie.aspx?username=" + username;
            Response.Redirect(url);

        }

        protected void btn_Final_Click(object sender, EventArgs e)
        {
            long screenID = Convert.ToInt64(Request.Params["screenID"]);
            long movieID = Convert.ToInt64(Request.Params["movieID"]);
            long userID = Convert.ToInt64(Request.Params["userdID"]);
            long STID = Convert.ToInt64(Request.Params["STID"]);
            string username = Request.Params["username"].ToString();
            DateTime movieDate = Convert.ToDateTime(Request.Params["movieDate"]);
            Response.Redirect(String.Format("ConfirmBooking?screenID={0}&movieID={1}&STID={2}&movieDate={3}&username={4}", Server.UrlEncode(screenID.ToString()), Server.UrlEncode(movieID.ToString()), Server.UrlEncode(STID.ToString()), Server.UrlEncode(movieDate.ToShortDateString()), Server.UrlEncode(username)));




        }

        protected void btn_Cancel_Click1(object sender, EventArgs e)
        {
            string username = HttpContext.Current.User.Identity.Name.ToString();
            long movieID = Convert.ToInt64(Request.Params["movieID"]);
            Response.Redirect(String.Format("User_MovieInfo.aspx?username={0}&movieID={1}", Server.UrlEncode(username), Server.UrlEncode(movieID.ToString())));
        }
    }
}